<?php

namespace App\Models\Diary;

use App\Libs\Helper\CommonHelper;
use \App\Models\User\User;
class Comment extends \App\Libs\Base\BaseModel
{

    protected $__TABLE__ = 't_paile_diary_comment';

    /**
     *    跳过检测了
     */
    public function beforeValidation()
    {
        self::skipValidation(['type'], $this);
    }

    public function getChildComment($pidArray)
    {
        $result = $this->modelsManager->createBuilder()
            ->from('Comment')->inWhere('fid', $pidArray)->getQuery()->execute();
        $robots = $this->modelsManager->createBuilder()
            ->from('Comment')
            ->inWher('id', $pidArray)
            ->getQuery()
            ->execute();
        var_dump($result);
        var_dump($robots);
    }

    public static function getCmt($diaryId, $page, $commentNum = 5)
    {

        //先找到顶级评论，转换结构；
        $commentData = Comment::findAll([
            'conditions' => ' did = ?1 and fid=0',
            'order' => 'ctime desc',
            'bind' => [1 => $diaryId],
            'offset' => ($page - 1) * $commentNum,
            'limit' => $commentNum
        ])->toArray();
        if (!empty($commentData)) {
            //转换评论结构，获取评论id，获取用户uid
            foreach ($commentData as $val) {
                $topComment[$val['id']] = $val;
                $topCommentId[] = $val['id'];
                $topUser[] = $val['uid'];
            }

            //获取顶级评论的用户信息；
            $userModel = new User();
            $ufoAndCmt = $userModel->mergeUfoAndCmt($topUser, $topComment);
            //得到最后的顶级评论
            $topTransCmt = CommonHelper::setColToKey($ufoAndCmt, 'id');

            //寻找子集评论；
            $childCmt = Comment::findAll([
                'conditions' => 'fid IN ({orderIdList:array})',
                'columns' => 'id,uid,content,fid',
                'bind' => ['orderIdList' => $topCommentId]
            ])->toArray();
            //存在多个评论需要合并：
            if (!empty($childCmt)) {
                $childCmtTrans = CommonHelper::setColToKey($childCmt, 'id');
                //获得评论用户id
                foreach ($childCmt as $key => $val) {
                    $childCmtId[] = $val['uid'];
                }
                //合并评论与用户信息
                $childUfoAndCmt = $userModel->mergeUfoAndCmt($childCmtId, $childCmtTrans);
                //合并多个子评论
                foreach ($childUfoAndCmt as $key => $val) {
                    $finalChildCmt[$val['fid']][] = $val;
                }
            }
            //组合二级评论
            foreach ($topTransCmt as $key => $val) {
                if (isset($finalChildCmt[$key])) {
                    $topTransCmt[$key]['childCmt'] = $finalChildCmt[$key];
                }
            }
            return $topTransCmt;
        } else {
            return '';
        }

    }

    public function insertNewComment()
    {

        $builder = Phalcon\Mvc\Model\Query\Builder::createInsertBuilder();


        $builder->table($this->__TABLE__)
            ->columns(array('name'))
            ->values(array(array('name' => 'Baidu')))
            ->getQuery()
            ->execute();
    }

}




































